from dbUtil import dbUtil


class musicService:
    def __init__(self):
        self.user_id = None
    def login(self, name, password):
        sql = 'select * from m_user where name=%s and password=%s'
        result = dbUtil().query_one(sql, name, password)
        self.user_id = result[0]
        return result

    def add_music(self, files):
        for file in files:
            start = file.rfind("/") + 1
            end = file.rfind(".")
            music_name = file[start:end]
            print(music_name)
            sql = 'insert into m_music (music_name,path) values (%s,%s)'
            mid = dbUtil().execute_dml(sql, music_name, file)
            sql = 'insert into m_list (uid,mid) values  (%s,%s)'
            dbUtil().execute_dml(sql, self.user_id, mid)
